package linkedList;

//单向链表 直接直接添加到链表尾部
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
        HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
        //创建一个链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);
        singleLinkedList.list();
    }
}
//SingleLinkedList
class SingleLinkedList {
    //初始化一个头结点 不存放数据
    private HeroNode head = new HeroNode(0, "", "");

    //添加节点到单向链表 当不考虑编号顺序 找到链表最后，指向新的节点
    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (true) {
            //找到最后
            if (temp.next == null) {
                break;
            }
            //没有找到
            temp = temp.next;
        }
        //退出while循环 temp就指向了链表最后
        temp.next = heroNode;
    }

    //显示链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head;
        while (true) {
            //找到最后
            if (temp.next == null) {
                break;
            }
            System.out.println(temp);
            //后移
            temp = temp.next;
        }
    }
}

//定义HeroNode，每个HeroNode对象就是一个节点
class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
